/**
 * 二次处理包装一些原型
 * @Author: liruilin 1443254530@qq.com
 * @Date: 2025-4-8 14:02:26
 * @LastEditors: liruilin 1443254530@qq.com
 * @FilePath: libs\common\index.propto.js
 */
import $func from '@/libs/utils/index.js'

const install = function(){
    /**
     * 解决四舍五入不准确的问题
     */
    if (!Number.prototype._toFixed) {
        Number.prototype._toFixed = Number.prototype.toFixed;
    }
    // Number.prototype.toFixed = function(n) {
    //     return parseFloat((this + 3e-16)._toFixed(n));
    // };
    Number.prototype.toFixed =  function(size) {
        let data = this
        let abs = 1;
        if (data < 0) abs = -1;
        const _data = Math.abs(data);
        let factor = Math.pow(10, size);
        let result = Math.round(_data * factor) / factor;
        // 这里使用toFixed的原因是为了防止舍入后round自动去除多余0的情况，
        //这样达不到要求的小数位数，例如Math.round(1.4101, 3)为1.41而不是1.410，
        //因此使用toFixed的作用仅仅是补充缺失的0
        return (Number(result) * abs)._toFixed(size).toString();
    }
    
    /**
     * finally
     * promise.then或promise.catch执行后立即执行
     * @param {Object} callback
     */
    Promise.prototype.finally = function (callback) {
        let P = this.constructor;
        return this.then(
          value  => P.resolve(callback()).then(() => value),
          reason => P.resolve(callback()).then(() => { throw reason })
        );
    }
    
    
    /**
     * 日期格式转换
     * @param {String} fmt 转换格式yyyy=年,MM=月,dd=日,EE=周(EEE=星期),HH=24h,hh=12h
     * @example new Date().format('yyyy年MM月dd日 EE HH:mm:ss');
     */
    Date.prototype.format = function(fmt) {
    	return $func.formatDate(this, fmt)
    }
    
    // #ifndef APP-NVUE
    /**
     * File对象兼容
     */
    try {
        new File([], '')
    } catch(e) {
        class File extends Blob {
            constructor(chunks, filename, opts = {}){
                super(chunks, opts)
                this.lastModifiedDate = new Date()
                this.lastModified =+ this.lastModifiedDate
                this.name = filename
            }
        }
    }
    // #endif
    
}

export default {
    install
}